package com.guinea.skoru.service;
// Generated 2011-3-19 15:17:31 by Hibernate Tools 3.4.0.Beta1

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;

import com.guinea.core.Constants;
import com.guinea.core.jdbc.JdbcDao;
import com.guinea.core.jdbc.PropertyFilterForSql;
import com.guinea.core.orm.Page;
import com.guinea.core.orm.hibernate.HibernateDao;
import com.guinea.core.service.CRUDManager;
import com.guinea.core.utils.CommonUtils;
import com.guinea.core.utils.StringUtils;
import com.guinea.skoru.dao.SkoruApplyDao;
import com.guinea.skoru.entity.SkoruApply;
import com.guinea.skosite.vo.SkoruApplyListVo;

public class SkoruApplyManager extends CRUDManager {

	private SkoruApplyDao skoruApplyDao;
	private JdbcDao jdbcDao;

	public HibernateDao getEntityDao() {
		return getSkoruApplyDao();
	}

	public SkoruApply getSkoruApply(Serializable id) {
		return (SkoruApply)getSkoruApplyDao().get(id);
	}

	public SkoruApplyDao getSkoruApplyDao() {
		return skoruApplyDao;
	}

	public void setSkoruApplyDao(SkoruApplyDao skoruApplyDao) {
		this.skoruApplyDao = skoruApplyDao;
	}
	
	

	public JdbcDao getJdbcDao() {
		return jdbcDao;
	}

	public void setJdbcDao(JdbcDao jdbcDao) {
		this.jdbcDao = jdbcDao;
	}

	public List getResumesByApplyId(Serializable id){
		String hql = "select r from SkoruResume r where r.applyId = ? and r.status = '1'";
		return getSkoruApplyDao().find(hql,new Object[]{id});
	}

	public List getRegionsByApplyId(Serializable id){
		String hql = "select r from SkoruRegion r where r.applyId = ? and r.status = '1'";
		return getSkoruApplyDao().find(hql,new Object[]{id});
	}

	public List getShareholdersByApplyId(Serializable id){
		String hql = "select s from SkoruShareholder s where s.applyId = ? and s.status = '1'";
		return getSkoruApplyDao().find(hql,new Object[]{id});
	}
//	public SkoruApply getApplyByRegionId(Serializable id){
////		String hql = "select a from SkoruRegion r left join SkoruApply a on r.applyId = a.id where r.id = ? and r.status = '1'";
////		return (SkoruApply)getSkoruApplyDao().findUnique(hql,new Object[]{id});
//		return null;
//	}
	public Page getApplications(Page page,List filters){
		String sql = "";
		sql += " select r.id region_id,a.id apply_id,r.province_id,r.city_id,r.district_id,a.created,a.corp,a.is_submitted";
		sql += " from skoru_region r , skoru_apply a";
		sql += " where a.id = r.apply_id(+) and a.status = '1'";
		Long userId = CommonUtils.getCurrentUserId();
		if(userId == null) userId = new Long(0);
		sql += " and a.creator = " + userId.longValue();
		if(!StringUtils.isBlank(PropertyFilterForSql.getProperty("",filters)))
			sql += "";
		if(!StringUtils.isBlank(page.getOrderBy()))
			sql += "";
		List l = jdbcDao.getJdbcTemplate().query(sql,new Object[]{},new RowMapper(){
			public Object mapRow(ResultSet rs, int i) throws SQLException {
				SkoruApplyListVo a = new SkoruApplyListVo();
				a.setApplyId(new Long(rs.getLong("APPLY_ID")));
				a.setCorp(rs.getString("CORP"));
				a.setSubmitDate(Constants.DEFAULT_DATE_FORMAT.format(rs.getDate("CREATED")));
				a.setProvinceId(rs.getString("PROVINCE_ID"));
				a.setCityId(rs.getString("CITY_ID"));
				a.setDistrictId(rs.getString("DISTRICT_ID"));
				if(rs.getLong("REGION_ID") > 0)
					a.setRegionId(new Long(rs.getLong("REGION_ID")));
				a.setIsSubmitted(rs.getString("IS_SUBMITTED"));
				return a;
			}
		});
		page.initFormList(l);
		return page;
	}
	public List getLatestApply(){
		Long userId = CommonUtils.getCurrentUserId();
		if(userId == null) userId = new Long(0);
		return getSkoruApplyDao().createQuery("select a from SkoruApply a where a.creator = ? and a.status='1' and a.isSubmitted = '1' order by a.submitDate desc",userId).list();
	}
}
